package com.hellobike.haxing.modules.wms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hellobike.haxing.common.annotation.AutoLog;
import com.hellobike.haxing.common.system.base.controller.BaseController;
import com.hellobike.haxing.common.system.query.QueryGenerator;
import com.hellobike.haxing.common.vo.Result;
import com.hellobike.haxing.modules.wms.entity.CarDriver;
import com.hellobike.haxing.modules.wms.entity.CarOut;
import com.hellobike.haxing.modules.wms.entity.TransportCar;
import com.hellobike.haxing.modules.wms.enums.CarOutStatusEnum;
import com.hellobike.haxing.modules.wms.service.ICarDriverService;
import com.hellobike.haxing.modules.wms.service.ICarOutService;
import com.hellobike.haxing.modules.wms.service.ITransportCarService;
import com.hellobike.haxing.modules.wms.utils.WmsCodeGeneratorUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
* @Description: t_wms_car_out
* @Author: haxing-boot
* @Date:   2020-02-02
* @Version: V1.0
*/
@RestController
@RequestMapping("/wms/carOut")
@Slf4j
public class CarOutController extends WmsBaseController<CarOut, ICarOutService> {
   @Autowired
   private ICarOutService carOutService;

   /**
    * 分页列表查询
    *
    * @param carOut
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   @AutoLog(value = "t_wms_car_out-分页列表查询")
   @GetMapping(value = "/list")
   public Result<?> queryPageList(CarOut carOut,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                  HttpServletRequest req) {
       carOut.setWarehouseId(getWarehouseId());
       QueryWrapper<CarOut> queryWrapper = QueryGenerator.initQueryWrapper(carOut, req.getParameterMap());
       Page<CarOut> page = new Page<CarOut>(pageNo, pageSize);
       IPage<CarOut> pageList = carOutService.page(page, queryWrapper);
       return Result.ok(pageList);
   }

   /**
    *   添加
    *
    * @param carOut
    * @return
    */
   @AutoLog(value = "t_wms_car_out-添加",operateType=2)
   @PostMapping(value = "/add")
   public Result<?> add(@RequestBody CarOut carOut) {
       Long warehouseId = getWarehouseId();
       carOut.setWarehouseId(warehouseId);
       carOut.setCarOutCode(WmsCodeGeneratorUtil.createCarOutCode(warehouseId));
       carOut.setCarOutStatus(CarOutStatusEnum.INIT.getValue());
       carOutService.save(carOut);
       return Result.ok("添加成功！");
   }

   /**
    *  编辑
    *
    * @param carOut
    * @return
    */
   @AutoLog(value = "t_wms_car_out-编辑",operateType=3)
   @PutMapping(value = "/edit")
   public Result<?> edit(@RequestBody CarOut carOut) {
       carOutService.updateById(carOut);
       return Result.ok("编辑成功!");
   }

   /**
    *   通过id删除
    *
    * @param id
    * @return
    */
   @AutoLog(value = "t_wms_car_out-删除",operateType=4)
   @DeleteMapping(value = "/delete")
   public Result<?> delete(@RequestParam(name="id",required=true) Long id) {
       carOutService.removeById(id);
       return Result.ok("删除成功!");
   }

   /**
    *  批量删除
    *
    * @param ids
    * @return
    */
   @AutoLog(value = "t_wms_car_out-批量删除",operateType=4)
   @DeleteMapping(value = "/deleteBatch")
   public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
       this.carOutService.removeByIds(Arrays.asList(ids.split(",")));
       return Result.ok("批量删除成功!");
   }

   /**
    * 通过id查询
    *
    * @param id
    * @return
    */
   @GetMapping(value = "/queryById")
   public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
       CarOut carOut = carOutService.getById(id);
       if(carOut==null) {
           return Result.error("未找到对应数据");
       }
       return Result.ok(carOut);
   }


    @GetMapping(value = "/queryByCode")
    public Result<?> queryByCode(@RequestParam(name="code",required=true) String code) {
        CarOut carOut = carOutService.getByCarOutCode(code);
        if(carOut==null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(carOut);
    }
   /**
   * 导出excel
   *
   * @param request
   * @param carOut
   */
   @AutoLog(value = "t_wms_car_out-导出excel",operateType=6)
   @RequestMapping(value = "/exportXls")
   public ModelAndView exportXls(HttpServletRequest request, CarOut carOut) {
       return super.exportXls(request, carOut, CarOut.class, "t_wms_car_out");
   }

   /**
     * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
   @AutoLog(value = "t_wms_car_out-导入excel",operateType=5)
   @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
   public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
       return super.importExcel(request, response, CarOut.class);
   }

   @Autowired
   private ITransportCarService transportCarService;
   @Autowired
   private ICarDriverService carDriverService;


    @GetMapping(value = "/getCars")
    public Result<?> getCars() {
        TransportCar car = new TransportCar();
        car.setWarehouseId(getWarehouseId());
        List<TransportCar> transportCars = transportCarService.getBaseMapper().selectList(new QueryWrapper<>(car));
        return Result.ok(transportCars);
    }

    @GetMapping(value = "/getDrivers")
    public Result<?> getDrivers() {
        CarDriver car = new CarDriver();
        car.setWarehouseId(getWarehouseId());
        List<CarDriver> drivers = carDriverService.getBaseMapper().selectList(new QueryWrapper<>(car));
        return Result.ok(drivers);
    }

}
